<?php

namespace App\Jobs\Message;

use App\Service\OpenShop\Feishu\MessageSendService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class FsMessageJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private string $feishuId;
    private array  $templateArr;
    private string $module;
    private int $microseconds;

    /**
     * 飞书推送job
     *
     * @return void
     */
    public function __construct(string $feishuId, array $templateArr, string $module, int $microseconds = 0)
    {
        $this->feishuId    = $feishuId;
        $this->templateArr = $templateArr;
        $this->module      = $module;
        $this->microseconds = $microseconds;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $do = MessageSendService::sendMessage($this->feishuId, 'user_id', $this->templateArr, 'interactive',
            $this->module);
        Log::info("FsMessageJob " . $this->feishuId.'-'.$this->module, (array)$do);

//        dump($do);

        if ($this->microseconds > 0) {
            usleep($this->microseconds);
        }
    }
}
